package org.sgx.gwteditors.testgallery.gallery.app;

import java.util.Map;

import org.sgx.gwteditors.client.util.GUIUtil;
import org.sgx.gwteditors.testgallery.gallery.Test;
import org.sgx.gwteditors.testgallery.gallery.TestResources;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.resources.client.ResourceCallback;
import com.google.gwt.resources.client.ResourceException;
import com.google.gwt.resources.client.TextResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.MenuItem;

public class Gallery2 extends Composite {

	private static Gallery2 instance;


	public static Gallery2 getInstance() {
		if (instance == null)
			instance = new Gallery2();
		return instance;
	}
	
	
	private static Gallery2UiBinder uiBinder = GWT.create(Gallery2UiBinder.class);
	@UiField VerticalPanel buttonPanel, testPanel;	
	@UiField FlowPanel toolbarPanel;
	@UiField MenuItem menuHelpAbout;
	private Test currentTest;
	interface Gallery2UiBinder extends UiBinder<Widget, Gallery2> {
	}

	private Gallery2() {
		initWidget(uiBinder.createAndBindUi(this));
		getToolbarPanel();
		getButtonPanel();
		getTestPanel(); 
		
		menuHelpAbout.setScheduledCommand(new ScheduledCommand() {			
			@Override
			public void execute() {
				ExternalTextResource r = TestResources.INSTANCE.about_html();
				try {
					r.getText(new ResourceCallback<TextResource>() {							
						@Override
						public void onSuccess(TextResource resource) {
							GUIUtil.acceptDialog("About", new HTML(resource.getText())); 
//							GUIUtil.showText(getCurrentTest().getName()+"'s java class source code: ",
//									resource.getText());
//									getCurrentTest().getJavaSources()[0]); //TODO	
						}
						
						@Override
						public void onError(ResourceException e) {
							Window.alert("Error getting text sources about_html"); 
						}
					});
				} catch (ResourceException e) {
					Window.alert("Error getting text RES about_html"); 
					e.printStackTrace();
				} 
			}
		}); 
//		
//		GalleryUtil.getInstance().checkUrl(); 
	}

	

private Widget getToolbarPanel() {
	
//	if(toolbarPanel==null) {
//		toolbarPanel=new FlowPanel();
//		toolbarPanel.setWidth("100%");
		toolbarPanel.add(new Button("test description", new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				GUIUtil.showMessage(getCurrentTest().getName()+"'s description: ", 
						getCurrentTest().getDescription());
			}
		}));
		toolbarPanel.add(new Button("about this app...", new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
			}
		}));
		toolbarPanel.add(new Button("test source", new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				doShowCurrentTestSources();		
			}
		}));
//	}
	return toolbarPanel;
}

public void setCurrentTest(Test currentTest) {
	this.currentTest = currentTest;
}

public void doShowCurrentTestSources() {
	doShowTestSources( getCurrentTest()); 	
}


public static void doShowTestSources(final Test t) {
	Map<String, ExternalTextResource> src = t.getJavaSources();
	if(src.keySet().size()>0) {
		final String rname = src.keySet().iterator().next();
		ExternalTextResource r = src.get(rname);
		try {
			r.getText(new ResourceCallback<TextResource>() {							
				@Override
				public void onSuccess(TextResource resource) {
					GUIUtil.showText(t.getName()+"'s java class source code: ",
							resource.getText());
//							getCurrentTest().getJavaSources()[0]); //TODO	
				}				
				@Override
				public void onError(ResourceException e) {
					Window.alert("Error getting text sources for resource "+rname); 
				}
			});
		} catch (ResourceException e) {
			Window.alert("Error getting text sources for resource "+rname); 
			e.printStackTrace();
		} 
	}		
}



protected Test getCurrentTest() {
	return currentTest;
}

public Widget getButtonPanel() {
//	if(buttonPanel==null) {
//		buttonPanel = new FlowPanel();
//		buttonPanel.setWidth("140px");
		for(final Test test : GalleryUtil.getInstance().getAllTests()) {
			Button b = new Button(test.getName(), new ClickHandler() {				
				@Override
				public void onClick(ClickEvent event) {
					doRunTest(test);
				}
			});
			buttonPanel.add(b);
		}
//	}
	return buttonPanel;
}

public void doRunTest(Test test) {
	testPanel.clear();
	currentTest = test;
	test.test(testPanel);
}


public Widget getTestPanel() {
//	if(testPanel==null) {
//		testPanel = new FlowPanel();
//		testPanel.setWidth("100%");
//	}
	return testPanel;
}

}
